package com.imay.live.view.custom;

import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.live.R;
import com.imay.live.utils.ViewUtil;

/**
 * Created by chan on 2017/6/29 0029.
 */

public class RoomChallengeView extends RelativeLayout implements View.OnClickListener {

    private TextView mChallengeTitle, mChallengeContentTv;
    private ImageView mIvShort;
    private LinearLayout mLinear;
    private boolean mShowChallengeContent;

    private OnChallengeShowListener mListener;

    public RoomChallengeView(Context context) {
        super(context);
        init(context);
    }

    public RoomChallengeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RoomChallengeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.view_room_short_detail, this);
        initWidget();
        initListener();
    }

    private void initWidget() {
        mLinear = ViewUtil.findView(this, R.id.ll_room_short_title);
        mChallengeTitle = ViewUtil.findView(this, R.id.tv_room_short_title);
        mIvShort = ViewUtil.findView(this, R.id.iv_room_short);
        mChallengeContentTv = ViewUtil.findView(this, R.id.room_short_tv_content);
        mChallengeContentTv.setMovementMethod(ScrollingMovementMethod.getInstance());
        mShowChallengeContent = mChallengeContentTv.getVisibility() == VISIBLE;
    }

    private void initListener() {
        mLinear.setOnClickListener(this);
        mIvShort.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_room_short_title:
            case R.id.iv_room_short:
                boolean visible = !mShowChallengeContent;
                setChallengeContentVisibility(visible);
                mListener.onChallengeShow(visible);
                break;
        }
    }

    /**
     * 设置内容显示还是隐藏
     *
     * @param visible
     */
    public void setChallengeContentVisibility(boolean visible) {
        mShowChallengeContent = visible;
        int visibility = visible ? VISIBLE : INVISIBLE;
        ViewUtil.startVisibilityAnimation(mChallengeContentTv, visibility);
        if (visibility == VISIBLE) {
            mIvShort.setSelected(false);
        } else if (visibility == INVISIBLE) {
            mIvShort.setSelected(true);
        }
    }

    /**
     * 设置标题
     *
     * @param challengeTitle
     */
    public void setChallengeTitle(String challengeTitle) {
        mChallengeTitle.setText(challengeTitle);
    }

    public boolean isShowChallengeContent() {
        return mShowChallengeContent;
    }

    /**
     * 设置内容
     *
     * @param challengeContent
     */
    public void setChallengeContent(String challengeContent) {
        mChallengeContentTv.setText(challengeContent);
    }

    public void setOnChallengeShowListener(OnChallengeShowListener listener) {
        mListener = listener;
    }

    public interface OnChallengeShowListener {
        void onChallengeShow(boolean show);
    }
}
